package com.raitcon.hibernate.backbean;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.raitcon.hibernate.bean.Departamento;
import com.raitcon.hibernate.bean.Pais;
import com.raitcon.hibernate.db.DepartamentoDB;
import com.raitcon.hibernate.db.PaisDB;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.richfaces.event.ItemChangeEvent;



/**
 *
 * @author Mike
 */
@ManagedBean(name="paisBean")
@RequestScoped
public class PaisBean implements  Serializable{
   protected static Logger log = Logger.getLogger(PaisBean.class); 
   private static  final  long serialVersionUID=-2377612760546575078L;
   //@ManagedProperty(value="#{currentItem}")
   private String currentItem = "";
   //@ManagedProperty(value="#{currentType}")
   private String currentType = "";
   private List<SelectItem> paisList = new ArrayList<SelectItem>();
   private List<SelectItem> departamentoList = new ArrayList<SelectItem>();
 
    public PaisBean() {
        
       
 
       
    }
    
    
    @PostConstruct
   protected void init(){
       log.debug("init...");
       PaisDB paisDB=new PaisDB();
        List<Pais> paisLst=paisDB.getPaisAll();
        SelectItem item = new SelectItem("", "--Selecciona Pais--");
        for(Pais p:paisLst){
            paisList.add(item);
            item = new SelectItem(p.getCodPais(), p.getDescripcion());
            
        }
      
   }
 
    public List<SelectItem> getPaistList() {
        return paisList;
    }
 
    public List<SelectItem> getDepartamentoList() {
        return departamentoList;
    }
    
    public void valueChanged(ValueChangeEvent event) {
        departamentoList.clear();
        if (null != event.getNewValue()) {
           // String[] currentItems;
            String codPais=(String)event.getNewValue();
            DepartamentoDB departamentoDB=new DepartamentoDB();
            List<Departamento>dptoList=departamentoDB.getDepartamentoByIdPais(codPais);
 
            for (Departamento dpto:dptoList) {
                SelectItem item = new SelectItem(dpto.getCodDepartamento(),dpto.getDescripcion());
                 departamentoList.add(item);
                 //FacesContext.getCurrentInstance().renderResponse();
            }
        }
    }
 
    public String getCurrentType() {
        return currentType;
    }
 
    public void setCurrentType(String currentType) {
        this.currentType = currentType;
    }
   
    public String getCurrentItem() {
        return currentItem;
    }
 
    public void setCurrentItem(String currentItem) {
        this.currentItem = currentItem;
    }
 
}
